package com.mosai.smartmedical.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Rays on 2020/12/25.
 */
public class SmsReceiver extends BroadcastReceiver {
    public static final String TAG = SmsReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive: ");
        SmsMessage[] smsMessages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
        if (smsMessages == null) {
            return;
        }
        for (SmsMessage smsMessage : smsMessages) {
            String messageBody = smsMessage.getMessageBody();
            String displayOriginatingAddress = smsMessage.getDisplayOriginatingAddress();
            String originatingAddress = smsMessage.getOriginatingAddress();

            Log.i(TAG, "onReceive:"
                    + " displayOriginatingAddress: " + displayOriginatingAddress
                    + " originatingAddress: " + originatingAddress
                    + " messageBody: " + messageBody
            );
            Pattern compile = Pattern.compile("Count#\\d+: Rs ([\\d.]+) paid by \\d{2}XXXX(\\d{4}) at .+Paytm");
            Matcher matcher = compile.matcher(messageBody);
            if (matcher.find()) {
                System.out.println(matcher.group(1));
                System.out.println(matcher.group(2));
            }
        }
    }

}
